﻿using System.Collections.Generic;
using TrueSync;
using Framework.AssetsManager;
public class LogicStateMachineComponent :  LogicComponent
{
    Dictionary<StateEnum, StateBase> mStateDict;
    public StateBase CurrentState { private set; get; }
    private SkillStreamState PredictSkillStream;

    LogicEntity mLEntity;
    LogicSkillGroupComponent mSkillGroupComponent;

    //是不是允许技能打断的开关,这个格外注意这个不允许做序列化
    public bool AllowSkillBreak;
    public bool IsTouchingRivalsGuardAlarmRect;

    public void Destroy()
    {
        CurrentState = null;
        mSkillGroupComponent = null;
    }

    public void DoUpdate(FP deltaTime)
    {
        if (PredictSkillStream != null)
        {
            PredictSkillStream.Excute(deltaTime);
            PredictSkillStream = null;
        }

        if (CurrentState != null)
        {
            CurrentState.Excute(deltaTime);
        }
    }

    public void Init(LogicEntity lEntity)
    {
        mLEntity = lEntity;
        mSkillGroupComponent = mLEntity.GetLogicComponent<LogicSkillGroupComponent>();
        mStateDict = new Dictionary<StateEnum, StateBase>();
        AllowSkillBreak = false;

        EByte info = LFileLoader.ReadFile("Txt/" + mLEntity.characterID + "/FSM");
        Deserialize(info);
    }

    public void Start()
    {
    }

    public void ChangeState(StateEnum nextStateName, params object[] stateParams)
    {
        if (mStateDict.ContainsKey(nextStateName) == false)
            return;

        StateBase nextState = mStateDict[nextStateName];
        if (CurrentState != null)
        {
            CurrentState.Exit();
            if(CurrentState.StateName == StateEnum.SkillStream)
            {
                //从技能流跳转到普通状态意味着对应的技能组也结束了,这里会存在被激励打断的情况
                mLEntity.GetLogicComponent<LogicSkillGroupComponent>().SkillGroupFinish();
            }
        }
            
        nextState.Enter(mLEntity, CurrentState, stateParams);
        CurrentState = nextState;
    }

    public void ChangeToSkillStream(int skillStreamID, params object[] stateParams)
    {
        SkillStreamState skillStream = mLEntity.GetLogicComponent<LogicSkillGroupComponent>().GetSkillStream(skillStreamID);
        if(skillStream != null)
            ChangeToSkillStream(skillStream, stateParams);
        else
            ChangeState(StateEnum.Stand);
    }

    public void ChangeToSkillStream(SkillStreamState skillStream, params object[] stateParams)
    {
        if (skillStream.IsPredictSkillStream) 
        {
            //是跳转技能流,把新的事件放进来
            skillStream.Predict(mLEntity, stateParams);
            PredictSkillStream = skillStream;
        }
        else
        {
            //正式打断之前的技能流然后进入新技能流,如果技能组变化了把技能组生效
            mSkillGroupComponent.EnterNewSkillGroup(skillStream.SkillGroupID);

            if (CurrentState != null)
                CurrentState.Exit();
            skillStream.Enter(mLEntity, CurrentState, stateParams);
            CurrentState = skillStream;
        }
    }

    void Deserialize(EByte ms)
    {
        while(ms.isEnd() == false)
        {
            string className = ms.ReadString();
            StateBase astate = (StateBase)ClassFactory.NewClassByName(className);
            astate.Deserialize(ms);
            mStateDict[astate.StateName] = astate;
        }
        
    }
}
